<?php

class AdzoneController extends BaseController
{
	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * 广告位菜单
	 */
	public function actionMenu()
	{
		$dataProvider=new CActiveDataProvider('Nav');
		$this->render('menu',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * 创建广告位
	 */
	public function actionCreateAdzone()
	{
		$model = new Adzone();
		//ajax数据验证
		$this->performAjaxValidation($model);

		if(isset($_POST['Adzone'])){
			//菜单id处理，只保存最低级别
			$model->zna_id = isset($_POST['Adzone']['zna_id2'])&&$_POST['Adzone']['zna_id2'] ? $_POST['Adzone']['zna_id2'] : $model->zna_id;
			$model->zna_id = isset($_POST['Adzone']['zna_id3'])&&$_POST['Adzone']['zna_id3'] ? $_POST['Adzone']['zna_id3'] : $model->zna_id;
			if($_POST['member'] && $model->zmg_id){
				$model->zmg_id = implode(',', $model->zmg_id);
			}elseif($_POST['member']){
				$model->zmg_id = '';
			}else{
				$model->zmg_id = 0;
			}
			$model->updatetime = $model->inputtime = time();
			$model->attributes = $_POST['Adzone'];
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin','zna_id'=>$model->zna_id));
			}else{				
				$this->render('../index/error',array(
						'message'=>'广告位添加失败！',						
				));
				exit;
			}
		}
		$model->zna_id = Yii::app()->request->getParam('zna_id');
		$menu1 = $menu2 = $menu3 = '';
		$menu1 = Nav::model()->findByPk($model->zna_id);
		//关联类型
		$model->zdz_associate = $menu1->zna_pagetype;
		//若存在父菜单
		if($menu1 && $menu1->zna_parentid!=0){
			$menu2 = Nav::model()->findByPk($menu1->zna_parentid);
			//保证$menu1是一级菜单,以此类推
			list($menu1,$menu2) = array($menu2,$menu1);
			if($menu1 && $menu1->zna_parentid!=0){
				$menu3 = Nav::model()->findByPk($menu1->zna_parentid);
				list($menu1,$menu2,$menu3) = array($menu3,$menu1,$menu2);
			}
		}
		$grouplist = MemGroup::model()->getMemGroup();
		//$grouplist = array_merge(array(0=>'所有会员组'),$grouplist);
		$this->render('create',array(
					'model'=>$model,
					'grouplist'=>$grouplist,
					'menu1'=>$menu1,
					'menu2'=>$menu2,
					'menu3'=>$menu3,				
				));
	}
	
	//ajax获取广告位列表下拉数据
	public function actionGetAdzoneList(){
		$data['menuid'] = Yii::app()->request->getParam('menuid',0);
		$data['level']  = Yii::app()->request->getParam('level',1);
		$data['items']  = Adzone::model()->getAdzonesForSelect($data['menuid']);
		$data['amount'] = count($data['items']);
		echo json_encode($data);
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate()
	{
		if(isset($_POST['Adzone'])){
			$model = Adzone::model()->findByPk($_POST['Adzone']['zdz_id']);
			//ajax数据验证
			$this->performAjaxValidation($model);
			
			$model->attributes = $_POST['Adzone'];
			//菜单id处理，只保存最低级别
			$model->zna_id = isset($_POST['Adzone']['zna_id2'])&&$_POST['Adzone']['zna_id2'] ? $_POST['Adzone']['zna_id2'] : $model->zna_id;
			$model->zna_id = isset($_POST['Adzone']['zna_id3'])&&$_POST['Adzone']['zna_id3'] ? $_POST['Adzone']['zna_id3'] : $model->zna_id;
			//print_r($model->zmg_id);exit;
			if($_POST['member'] && isset($_POST['Adzone']['zmg_id'])){
				$model->zmg_id = implode(',', $model->zmg_id);
			}elseif($_POST['member']){
				$model->zmg_id = null;
			}else{
				$model->zmg_id = 0;
			}
			$model->updatetime = time();
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin','zna_id'=>$model->zna_id));
			}else{
				$this->render('../index/error',array(
						'message'=>'广告位修改失败！',
						'links'=>array(
								array('广告位管理',$this->pre_module_url.'/adzone/admin/zna_id/'.$model->zna_id),
						),
				));
				exit;
			}
		}
		$model = $this->loadModel(Yii::app()->request->getParam('zdzid'));
		$model->zmg_id = $model->zmg_id ? explode(',', $model->zmg_id) : $model->zmg_id;
		$menu1 = $menu2 = $menu3 = '';
		$menu1 = Nav::model()->findByPk($model->zna_id);
		//若存在父菜单
		if($menu1 && $menu1->zna_parentid!=0){
			$menu2 = Nav::model()->findByPk($menu1->zna_parentid);
			//保证$menu1是一级菜单,以此类推
			list($menu1,$menu2) = array($menu2,$menu1);
			if($menu1 && $menu1->zna_parentid!=0){
				$menu3 = Nav::model()->findByPk($menu1->zna_parentid);
				list($menu1,$menu2,$menu3) = array($menu3,$menu1,$menu2);
			}
		}
		$grouplist = MemGroup::model()->getMemGroup();
		//$grouplist = array_merge(array(0=>'所有会员组'),$grouplist);
		$this->render('update',array(
					'model'=>$model,
					'grouplist'=>$grouplist,
					'menu1'=>$menu1,
					'menu2'=>$menu2,
					'menu3'=>$menu3,
				));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Adzone');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Adzone('search');
		$model->unsetAttributes();
		if(isset($_GET['Adzone']))
			$model->attributes=$_GET['Adzone'];
		//菜单id
		
		$zna_id[1] = isset($_GET['zna_id']) ? $_GET['zna_id'] : 0;
		$zna_id[2] = isset($_GET['zna_id2']) ? $_GET['zna_id2'] : 0;
		$zna_id[3] = isset($_GET['zna_id3']) ? $_GET['zna_id3'] : 0;
		$model->zna_id = !$zna_id[2] ? $zna_id[1] : $zna_id[2];
		$model->zna_id = !$zna_id[3] ? $model->zna_id : $zna_id[3];
		$model->zna_id = $model->zna_id<1 ? '' : $model->zna_id;
		//关键字
		$model->zdz_name = $model->zdz_name=='关键字' ? '' : trim($model->zdz_name);
		$this->render('admin',array(
			'zna_id'=>$zna_id,
			'model'=>$model,
		));
	}

	/**
	 * 批量操作：上下线、删除
	 */
	public function actionBatchChange()
	{
		$ids = Yii::app()->request->getParam('ids',0);
		$idbox = array(0);
		if(is_array($ids) && count($ids)){
			foreach($ids as $key=>$id){
				(int)$id && $idbox[] = (int)$id;
			}
		}elseif($ids){
			(int)$ids && $idbox[] = (int)$ids;
		}else{
			$this->render('../index/error',array(
					'message'=>'删除失败，未选中任何选项！',
					'links'=>array(
							array('广告位管理',$this->pre_module_url.'/adzone/admin'),
					),
			));
			exit;
		}
		if(count($idbox)>1 && Adzone::model()->batchChange($idbox)){
			//成功跳转
			$this->redirect(array('admin'));
		}else{
			$this->render('../index/error',array(
					'message'=>'操作失败！',
					'links'=>array(
							array('广告位管理',$this->pre_module_url.'/adzone/admin'),
					),
			));
			exit;
		}
	}
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Adzone::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='adzone-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
}
